import re
import urllib.request
from urllib import request
import time

def p(*args,**kvArgs):
    print(args,kvArgs)


p(123,456,33333,0000,'abc',{"k":123})



print("hello")


def getHtmlSourceCode(url):
    with request.urlopen(url) as f:
        data = f.read()
        p('status:',f.status,f.reason)
        for k,v in f.getheaders():
            p('%s: %s' % (k,v))
        # p('data:',data.decode('utf-8'))
        return data


def getHtml(url):
    p('start get html:')
    page = urllib.request.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    reg = r'src="(.+?\.jpg)" pic_ext'
    imgre = re.compile(reg)
    html = html.decode('utf-8')
    imgList = re.findall(imgre,html)
    p(imgList)
    x = 0

    for imgurl in imgList:
        # time.sleep(1)
        urllib.request.urlretrieve(imgurl,'./demo/%s.jpg' % x)
        x += 1


html = getHtmlSourceCode("https://tieba.baidu.com/p/2555125530")
# p(html)
p(getImg(html))

urllib.request.urlretrieve('https://imgsa.baidu.com/forum/w%3D580/sign=028476fbfcfaaf5184e381b7bc5694ed/df095b8da97739127f845bbcf9198618377ae25c.jpg','./demo/%s.jpg' % 'demo')
